package com.android.baseconfig.base;

import com.android.baseconfig.common.utils.Logc;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lei
 * @description ApiServiceManager
 * @date 2018/5/15
 */
public class ApiServiceManager {
    private static final String TAG = ApiServiceManager.class.getName();
    private static final Map<String, Constructor<?>> services = new HashMap();
    private static String c;

    private ApiServiceManager() {
    }

    public static <T> T getImpl(Class<T> classz) {
        if (classz == null) {
            return null;
        } else {

            try {
                String classzName = classz.getName();
                Constructor constructor;
                if (services.containsKey(classzName)) {
                    constructor = (Constructor) services.get(classzName);
                } else {
                    constructor = classz.getConstructor();
                    services.put(classzName, constructor);
                }

                return (T) constructor.newInstance();
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException |
                    NoSuchMethodException var2) {
                Logc.e(TAG, "", var2);
                return null;
            }
        }
    }
}
